//我的关注页面

import UIKit
import MJRefresh

class MYGMyFollowsViewController: MYGBaseController,UITableViewDelegate,UITableViewDataSource {
    var pageType:Int = 1
    var page = 1
    var dataSource = [MYGFriendsModel]()
    var total = ""
    
    lazy var contentTF:UITextField = {
        let followSearchBar = UITextField()
        followSearchBar.placeholder = "搜索用户备注或名字"
        followSearchBar.backgroundColor = RGBA(r: 243, g: 243, b: 243, a: 1)
        followSearchBar.layer.masksToBounds = true
        followSearchBar.layer.cornerRadius = SCALEWIDTH(17.5)
        followSearchBar.font = UIFont.systemFont(ofSize: SCALEWIDTH(14), weight: .light)
        followSearchBar.textColor = UIColor.init(hexString: "#333333")
        followSearchBar.textAlignment = .center
        followSearchBar.returnKeyType = .search
        followSearchBar.clearButtonMode = .whileEditing
        followSearchBar.delegate = self
        return followSearchBar
    }()
    
   lazy var tabView:UITableView = {
        let  control = UITableView(frame: CGRectZero, style: .grouped)
        control.delegate = self
        control.backgroundColor = .clear
        control.register(cellWithClass: MYGMyFollowsTableViewCell.self)
        control.dataSource = self
        control.separatorStyle = .none
        return control
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabView.mj_header?.beginRefreshing()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if pageType == 1 {
            //我的好友
            self.title = "我的好友"
        }else if pageType == 2 {
            //我的关注
            self.title = "我的关注"
        }else{
            //我的粉丝
            self.title = "我的粉丝"
        }
        navigationBarShow()
        initUI()
    }
    func initUI(){
        view.addSubview(contentTF)
        contentTF.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(16.5))
            make.right.equalToSuperview().offset(-SCALEWIDTH(16.5))
            make.top.equalToSuperview().offset(15)
            make.height.equalTo(SCALEWIDTH(35))
        }
       
        view.addSubview(tabView)
        tabView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(contentTF.snp.bottom).offset(SCALEWIDTH(10))
        }
        
        tabView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.page = 1
            self?.getList()
        })
        
        tabView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {[weak self] in
            self?.page += 1
            self?.getList()
        })
    }
    
     func getList() {
         MYGNetwork<MYGMeAPI, MYGPageModel<MYGFriendsModel>>.request(target: .myFriendsList(param: [
            "name":self.contentTF.text ?? "",
            "type":"\(self.pageType)",
            "page":"\(self.page)",
            "pageSize":"20"])) { item in
                
                self.tabView.mj_header?.endRefreshing()
                if item.page.page == 1 {
                    self.dataSource = item.list
                } else {
                    self.dataSource.append(contentsOf: item.list)
                }
                
                self.total = "\(item.page.total)"
                
                if item.page.total <= self.dataSource.count {
                    self.tabView.mj_footer?.endRefreshingWithNoMoreData()
                } else {
                    self.tabView.mj_footer?.endRefreshing()
                }
                
            self.tabView.reloadData()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let vi = UIView()
        let titleLab = UILabel()
        titleLab.textColor = RGBA(r: 56, g: 56, b: 56, a: 1)
        titleLab.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        if pageType == 1{
            //我的好友
            titleLab.text = "朋友"
        }else if pageType == 2{
            //我的关注
            titleLab.text = "我的关注（" + total + "人）"
        }else{
            //我的粉丝
            titleLab.text = "我的粉丝（" + total + "人）"
        }
        vi.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.height.equalTo(SCALEWIDTH(20))
        }
        return vi
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return SCALEWIDTH(40)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 65
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MYGMyFollowsTableViewCell", for: indexPath) as! MYGMyFollowsTableViewCell 
        cell.selectionStyle = .none
        var model = self.dataSource[indexPath.row]
        model.type = self.pageType
        cell.bindData(model)
        cell.tapFollowCompletion = {[weak self] in
            self?.tabView.mj_header?.beginRefreshing()
        }
        return cell
    }
}

extension MYGMyFollowsViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        self.tabView.mj_header?.beginRefreshing()
        return true
    }
}
